package com.woniu.service.impl;

import com.woniu.dao.PermissionDao;
import com.woniu.dao.RoleDao;
import com.woniu.dao.RolepermissionDao;
import com.woniu.pojo.*;
import com.woniu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RolepermissionDao rolepermissionDao;
    @Override
    public List<Role> getRolersByPage(PageBean pageBean, Role role) {
        //先封装pageBean的总页数和总行数
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if(role!=null&&role.getRname()!=null&&!"".equals(role.getRname())) {
            criteria.andRnameLike("%" + role.getRname() + "%");
        }
        Long l = roleDao.countByExample(roleExample);
        pageBean.setCountRow(l.intValue());
        pageBean.setCountPage(pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1);
        //分页功能
        roleExample.setOffset((long) ((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        roleExample.setLimit(pageBean.getPageSize());
        List<Role> roleList = roleDao.selectByExample(roleExample);

        return roleList;
    }

    @Override
    public List<Permission> getPermissionByRid(Integer rid) {
        RolepermissionExample example = new RolepermissionExample();
        example.createCriteria().andRidEqualTo(rid);
        List<Rolepermission> rolepermissionList = rolepermissionDao.selectByExample(example);
        List<Integer> pids = new ArrayList<>();
        for (Rolepermission rp:rolepermissionList) {
            pids.add(rp.getPid());
        }

        PermissionExample example2 = new PermissionExample();
        example2.createCriteria().andPidIn(pids);
        List<Permission> permissionList2 = permissionDao.selectByExample(example2);


        List<Permission> permissionList = permissionDao.selectByExample(null);
        Map<Integer,Permission> permissionMap = new HashMap<>();
        for(Permission permission:permissionList){
            if(permissionList2.contains(permission)){
                permission.setChecked(true);
            }
            permissionMap.put(permission.getPid(),permission);
        }
        List<Permission> rootList = new ArrayList<>();

        for(Permission permission:permissionList){
            if(permission.getParentid()==null){
                rootList.add(permission);
                permission.setOpen(true);
            }else{
                permissionMap.get(permission.getParentid()).getChildren().add(permission);
            }
        }
        return rootList;
    }
}
